#include "Tools/RIVIABlurOrSharpenTool.h"
#include "IRIVIABrushes.h"
#include "IRIVIAProject.h"

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"

#include "Thread/RIVIAThreadInterface.h"

#include <QTime>
#include <QDebug>

using namespace cv;

RIVIABlurOrSharpenTool::RIVIABlurOrSharpenTool()
{

}

RIVIABlurOrSharpenTool::~RIVIABlurOrSharpenTool()
{

}

void RIVIABlurOrSharpenTool::excuteBlur(int nStartRow, int nEndRow)
{
    for(int j = nStartRow; j <= nEndRow; ++j)
    {
        for(int i = 1; i < m_roi_mat.cols-1; ++i)
        {
            if(m_brushroi_mat.at<uchar>(j,i) != 0)
            {
                m_roi_matproc.at<Vec3b>(j,i)[0] = saturate_cast<uchar>((
                            m_roi_mat.at<Vec3b>(j, i)[0]
                        + m_roi_mat.at<Vec3b>(j-1, i)[0]
                        + m_roi_mat.at<Vec3b>(j+1, i)[0]
                        + m_roi_mat.at<Vec3b>(j, i-1)[0]
                        + m_roi_mat.at<Vec3b>(j, i+1)[0])/5);
                m_roi_matproc.at<Vec3b>(j,i)[1] = saturate_cast<uchar>((
                            m_roi_mat.at<Vec3b>(j, i)[1]
                        + m_roi_mat.at<Vec3b>(j-1, i)[1]
                        + m_roi_mat.at<Vec3b>(j+1, i)[1]
                        + m_roi_mat.at<Vec3b>(j, i-1)[1]
                        + m_roi_mat.at<Vec3b>(j, i+1)[1])/5);
                m_roi_matproc.at<Vec3b>(j,i)[2] = saturate_cast<uchar>((
                            m_roi_mat.at<Vec3b>(j, i)[2]
                        + m_roi_mat.at<Vec3b>(j-1, i)[2]
                        + m_roi_mat.at<Vec3b>(j+1, i)[2]
                        + m_roi_mat.at<Vec3b>(j, i-1)[2]
                        + m_roi_mat.at<Vec3b>(j, i+1)[2])/5);
            }
        }
        m_numRows++;
    }
}

void RIVIABlurOrSharpenTool::applyOnScreen(int x, int y)
{
    updateROI(x, y);

    //    cv::GaussianBlur(roi_mat, roi_matproc, cv::Size(3, 3), 0, 0);

//    Mat kernel = (Mat_<char>(3,3) <<  0, -1,  0,
//                                   -1,  5, -1,
//                                    0, -1,  0);
//    filter2D( roi_mat, roi_matproc, roi_mat.depth(), kernel );

//    auto startTime = QTime::currentTime();

    for(int j = 1; j < m_roi_mat.rows-1; ++j)
    {
        for(int i = 1; i < m_roi_mat.cols-1; ++i)
        {
            if(m_brushroi_mat.at<uchar>(j,i) != 0)
            {
                m_roi_matproc.at<Vec3b>(j,i)[0] = saturate_cast<uchar>((
                            m_roi_mat.at<Vec3b>(j, i)[0]
                        + m_roi_mat.at<Vec3b>(j-1, i)[0]
                        + m_roi_mat.at<Vec3b>(j+1, i)[0]
                        + m_roi_mat.at<Vec3b>(j, i-1)[0]
                        + m_roi_mat.at<Vec3b>(j, i+1)[0])/5);
                m_roi_matproc.at<Vec3b>(j,i)[1] = saturate_cast<uchar>((
                            m_roi_mat.at<Vec3b>(j, i)[1]
                        + m_roi_mat.at<Vec3b>(j-1, i)[1]
                        + m_roi_mat.at<Vec3b>(j+1, i)[1]
                        + m_roi_mat.at<Vec3b>(j, i-1)[1]
                        + m_roi_mat.at<Vec3b>(j, i+1)[1])/5);
                m_roi_matproc.at<Vec3b>(j,i)[2] = saturate_cast<uchar>((
                            m_roi_mat.at<Vec3b>(j, i)[2]
                        + m_roi_mat.at<Vec3b>(j-1, i)[2]
                        + m_roi_mat.at<Vec3b>(j+1, i)[2]
                        + m_roi_mat.at<Vec3b>(j, i-1)[2]
                        + m_roi_mat.at<Vec3b>(j, i+1)[2])/5);
            }
        }
    }

    m_roi_matproc.copyTo(m_roi_mat);
}
